package com.alibaba.sample.petstore.dal.dao.cached;

import java.util.List;
import java.util.Map;

import com.alibaba.sample.petstore.dal.dao.OrderDao;
import com.alibaba.sample.petstore.dal.dataobject.Order;

public class CachedOrderDao extends AbstractCachedDao<OrderDao> implements OrderDao {
    private final Map<Integer, Order> ordersById = createCache();
    private final Map<String, List> ordersByUsername = createCache();

    public CachedOrderDao(OrderDao backingDao) {
        super(backingDao);
    }

    public Order getOrder(int orderId) {
        Order order = ordersById.get(orderId);

        if (order == null) {
            order = backingDao.getOrder(orderId);
            ordersById.put(orderId, order);
        }

        return order;
    }

    public List getOrdersByUsername(String username) {
        List orderList = ordersByUsername.get(username);

        if (orderList == null) {
            orderList = backingDao.getOrdersByUsername(username);
            ordersByUsername.put(username, orderList);
        }

        return orderList;
    }

    public void insertOrder(Order order) {
        backingDao.insertOrder(order);
    }
}
